package com.th.supcom.pds.drugsupply.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.common.client.commom.PdsAuditPrescServiceFactory;
import com.th.supcom.pds.drugsupply.client.model.PdsPackageInfoModel;
import com.th.supcom.pds.drugsupply.client.service.PdsPackageMaintainRpcAsync;

/**
 * 供药分装维护主界面
 */
public class PdsSplitInfoMainPanel extends ContentPanel
{

    private static PdsPackageMaintainRpcAsync s_pdsPackageMaintainRpcAsync = PdsAuditPrescServiceFactory.getPhamSplitInfoService ();

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    /** 药品分装信息表格. */
    private DrugSplitEditorGridPanel m_drugSplitEditorGridPanel;

    public PdsSplitInfoMainPanel ()
    {
        init ();
        setStyleAttribute ("border", "0px");
        setStyleAttribute ("padding", "0px");
    }

    public void addInfo ()
    {
        final PdsPackageInfoModel model = new PdsPackageInfoModel ();
        model.setEnable ("1");
        model.setUnitTypeCode ("10");
        final ListStore <PdsPackageInfoModel> mListStore = m_drugSplitEditorGridPanel.getListStore ();
        mListStore.insert (model, 0);
        m_drugSplitEditorGridPanel.getGrid ().startEditing (0, 0);

    }

    private void alertMessage (final List <Long> serialNOlist)
    {
        MessageBox.confirm ("警告", "是否确认删除", new Listener <MessageBoxEvent> ()
        {
            @Override
            public void handleEvent (final MessageBoxEvent be)
            {
                final Button btn = be.getButtonClicked ();
                if (Dialog.YES.equals (btn.getItemId ()))
                {

                    PdsSplitInfoMainPanel.s_pdsPackageMaintainRpcAsync.deletePackageInfo (serialNOlist, new AsyncCallback <Void> ()
                    {

                        @Override
                        public void onFailure (final Throwable caught)
                        {
                            MessageBox.alert ("警告", caught.getMessage (), null);
                        }

                        @Override
                        public void onSuccess (final Void result)
                        {
                            Info.display ("提示", "删除成功");
                            refreshInfo ();
                        }
                    });

                }
            }
        });
    }

    public void deleteInfo ()
    {
        // 得到表格中的数据
        final List <PdsPackageInfoModel> models = m_drugSplitEditorGridPanel.getChkModel ().getSelectedItems ();
        final List <Long> serialNOlist = new ArrayList <Long> ();
        if (models.isEmpty ())
        {
            MessageBox.alert ("警告", "请选择你要删除的的数据", null);
            return;
        }
        for (final PdsPackageInfoModel pdsPackageInfoModel : models)
        {

            if ("0".equals (pdsPackageInfoModel.getFlag ()))
            {
                MessageBox.alert ("警告", "不能删除原包装信息", null);
                return;
            }
            if (pdsPackageInfoModel.getSerialNo () == null || "1".equals (pdsPackageInfoModel.getEnable ()))
            {
                pdsPackageInfoModel.setSerialNo (-1L);
            }

            serialNOlist.add (pdsPackageInfoModel.getSerialNo ());

        }
        alertMessage (serialNOlist);

    }

    public DrugSplitEditorGridPanel getDrugSplitEditorGridPanel ()
    {

        return m_drugSplitEditorGridPanel;
    }

    public List <PdsPackageInfoModel> getGridListModels ()
    {

        return m_drugSplitEditorGridPanel.getChkModel ().getSelectedItems ();

    }

    private void init ()
    {
        this.setHeaderVisible (false);
        this.setBodyBorder (false);
        setLayout (new BorderLayout ());
        // 初始化中心面板
        m_drugSplitEditorGridPanel = new DrugSplitEditorGridPanel ();
        final BorderLayoutData centerData = new BorderLayoutData (LayoutRegion.CENTER);
        centerData.setMargins (new Margins (0, 2, 2, 2));
        add (m_drugSplitEditorGridPanel, centerData);

    }

    /**
     * 修改信息
     * 
     * add by Zli
     */
    public void modifyInfo ()
    {
        final List <PdsPackageInfoModel> models = m_drugSplitEditorGridPanel.getChkModel ().getSelectedItems ();
        if (models.isEmpty ())
        {
            MessageBox.alert ("警告", "请选择你要保存的的数据", null);
            return;
        }
        for (final PdsPackageInfoModel pdsPackageInfoModel : models)
        {
            if (pdsPackageInfoModel.getPhamUnitName () == null)
            {
                MessageBox.alert ("警告", "请输入药品名称", null);
                return;
            }
        }

        PdsSplitInfoMainPanel.s_pdsPackageMaintainRpcAsync.modifyPackageInfo (models, new AsyncCallback <Void> ()
        {

            @Override
            public void onFailure (final Throwable caught)
            {

                MessageBox.alert ("警告", caught.getMessage (), null);
            }

            @Override
            public void onSuccess (final Void result)
            {
                Info.display ("提示", "保存成功");
                refreshInfo ();

            }
        });
    }

    public void queryInfo ()
    {

        m_drugSplitEditorGridPanel.loadGridData ();

    }

    /***
     * 刷新表格
     * 
     * add by Zli
     */
    public void refreshInfo ()
    {
        m_drugSplitEditorGridPanel.loadGridData ();
    }

}
